import 'dart:async';

///
/// 使用Stream 实现Java中的Observer工具
///

class EventBus{
  StreamController _streamController;

  StreamController get streamController => _streamController;

  EventBus({bool sync = false}):this._streamController = StreamController.broadcast(sync: sync);

  EventBus.customController(StreamController controller):_streamController = controller;

  ///返回Steam
  Stream<T> on<T>(){
    if(T == dynamic){
      return streamController.stream as Stream<T>;
    }else{
      return streamController.stream.where((event) => event is T).cast<T>();
    }
  }


  void fire(event){
    _streamController.add(event);
  }

  void destory(){
    _streamController.close();
  }
}

EventBus eventBus = EventBus();